<template>
  <div>
    <el-dialog
    :before-close="handleDialogClose"
    :destroy-on-close="true"
     :close-on-click-modal='false'
      title="导出数据"
      :visible.sync="exportVisible"
      width="40%"
      center
      >
      <el-row :gutter="10">
        <el-col >
          <span class="font">
              推送时间
          </span>
          <el-date-picker
            v-model="value2"
            type="datetimerange"
            :picker-options="pickerOptions"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            align="right"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            @change="deteChang"
          ></el-date-picker>
        </el-col>
        <el-col style="margin-top:20px;">
            <span class="font">
              账号类型
            </span>
             <el-select
              multiple
              filterable
              allow-create
              default-first-option
              v-model="value1" placeholder="请选择">
                <el-option
                  v-for="item in Role"
                  :key="item.id"
                  :label="item.role_name"
                  :value="item.id">
                </el-option>

              </el-select>
        </el-col>
      </el-row>
      <span slot="footer" class="dialog-footer">
        <el-button type="button" class="pubBtn2" @click="exportCancle">取消操作</el-button>
        <el-button type="button" class="pubBtn" @click="exportSave">确定导出</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {exporPushtUser,statisticsExport} from "@/api/admin"
  export default{
    props:['exportVisible','Role','Statistics'],
    data() {
      return {
        bentime: "", //起始时间
        endtime: "", //结束时间
        type: "",
        category_id: "",
        province: "",
        city: "",
        value2: "",
        options: [],
          value1:"",
        pickerOptions: {
          shortcuts: [
            {
              text: "最近一周",
              onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
                picker.$emit("pick", [start, end]);
              }
            },
            {
              text: "最近一个月",
              onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
                picker.$emit("pick", [start, end]);
              }
            },
            {
              text: "最近三个月",
              onClick(picker) {
                const end = new Date();
                const start = new Date();
                start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
                picker.$emit("pick", [start, end]);
              }
            }
          ]
        }
      };
    },
    methods:{
      deteChang(val){
        this.bentime =val[0];
        this.endtime =val[1];
      },
      exportSave(){
        if(this.Statistics ==1){
          let d =new Date(this.value2[0])
          statisticsExport({
            role_id: this.value1,
            start: this.bentime,
            end: this.endtime
          }).then(res => {
            const blob = new Blob([res.data]);
            const fileName = "数据统计.xls";
            const elink = document.createElement("a");
            elink.download = fileName;
            elink.style.display = "none";
            elink.href = URL.createObjectURL(blob);
            document.body.appendChild(elink);
            elink.click();
            URL.revokeObjectURL(elink.href); // 释放URL 对象
            document.body.removeChild(elink);
          });
          let exportVisible = false;
          this.$emit("Determine", exportVisible);
        }else{
          let d =new Date(this.value2[0])
          exporPushtUser({
            role_id: this.value1,
            bentime: this.bentime,
            endtime: this.endtime
          }).then(res => {
            const blob = new Blob([res.data]);
            const fileName = "专利费用预警信息.xls";
            const elink = document.createElement("a");
            elink.download = fileName;
            elink.style.display = "none";
            elink.href = URL.createObjectURL(blob);
            document.body.appendChild(elink);
            elink.click();
            URL.revokeObjectURL(elink.href); // 释放URL 对象
            document.body.removeChild(elink);
          });
          let exportVisible = false;
          this.$emit("Determine", exportVisible);
        }
      },
      exportCancle(){
        let exportVisible = false;
        this.$emit("Determine", exportVisible);
      },
      handleDialogClose(){
        this.$emit("Determine",false);
      }
    }
  }
</script>

<style lang="less" scoped>
  .font{
    padding-right:25px;
  }
</style>
